package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import model.Element;
import control.MapMouseListener;

public class BuildMenu extends JPanel{
	
	private JPanel menu;
	private int size = 0;
	private MapMouseListener ml;
	private Option selected;
	
	public BuildMenu(MapMouseListener ml){
		setOpaque(false);
		this.ml = ml;
		Dimension d = new Dimension(60,60);
		setSize(d);
		setPreferredSize(d);
	}

	public void addOption(Element e){
		size++;
		add(new Option(e));
		setLayout(new GridLayout(1, size));
	}
	
	private class Option extends JLabel implements MouseListener{

		private Element option;
		
		public Option(Element option){
			this.option = option;
			setOpaque(true);
			setBackground(option.getColor());
			addMouseListener(this);
			setBorder(BorderFactory.createLineBorder(Color.BLACK));
		}
		
		@Override
		public void mouseClicked(MouseEvent m) {
			this.setBorder(BorderFactory.createLineBorder(Color.WHITE));
			if(selected!=null){
				selected.setBorder(BorderFactory.createLineBorder(Color.BLACK));
				if(selected==this){
					selected = null;
					ml.select(null);
					return;
				}
			}
			ml.select(option);
			selected = this;
		}
		@Override
		public void mouseEntered(MouseEvent arg0) {
		}
		@Override
		public void mouseExited(MouseEvent arg0) {
		}
		@Override
		public void mousePressed(MouseEvent arg0) {
		}
		public void mouseReleased(MouseEvent arg0) {
		}
		
	}
	
}
